package 剑指offer;

import java.util.HashMap;
import java.util.Map;

public class _07重建二叉树 {
    Map<Integer,Integer> map = new HashMap<>();
    int[] preorder,inorder;
    int pre_index;
    public TreeNode fun(int start,int end){
        if (start > end){
            return null;
        }
        TreeNode root = new TreeNode(preorder[pre_index]);
        pre_index++;
        int mid = map.get(root.val);
        root.left = fun(start,mid - 1);
        root.right = fun(mid + 1,end);
        return root;
    }
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        this.preorder = preorder;
        this.inorder = inorder;
        int index = 0;
        pre_index = 0;
        for(int i : inorder){
            map.put(i,index++);
        }
        return fun(0,inorder.length - 1);
    }
}
